#include "stdafx.h"
#include "consoleio.h"

const wchar_t LINEFEED[] = L"\r\n";

/*static*/std::shared_ptr<ILog> CConsoleLog::CreateInstance()
{
	return std::make_shared<CConsoleLog>();
}

void CConsoleLog::WriteStdOut(const wchar_t* fmt, ...)
{
	va_list argptr;
	va_start(argptr, fmt);
	this->Write(stdout, fmt, argptr);
	va_end(argptr);
}

void CConsoleLog::WriteVStdErr(const wchar_t* fmt, va_list argptr)
{
	this->Write(stdout, fmt, argptr);
	this->WriteString(stderr, LINEFEED);
}

void CConsoleLog::WriteStdOutLine(const wchar_t* fmt, ...)
{
	va_list argptr;
	va_start(argptr, fmt);
	this->Write(stdout, fmt, argptr);
	this->WriteString(stdout, LINEFEED);
	va_end(argptr);
}

void CConsoleLog::WriteVStdOutLine(const wchar_t* fmt, va_list argptr)
{
	this->Write(stdout, fmt, argptr);
	this->WriteString(stdout, LINEFEED);
}

void CConsoleLog::WriteStdErr(const wchar_t* fmt, ...)
{
	va_list argptr; /* Set up the variable argument list here */
	va_start(argptr, fmt); /* Start up variable arguments */
	this->Write(stderr, fmt, argptr);
	va_end(argptr);
}

void CConsoleLog::WriteVStdErrLine(const wchar_t* fmt, va_list argptr)
{
	this->Write(stderr, fmt, argptr);
	this->WriteString(stderr, LINEFEED);
}

void CConsoleLog::WriteStdErrLine(const wchar_t* fmt, ...)
{
	va_list argptr;
	va_start(argptr, fmt);
	this->Write(stderr, fmt, argptr);
	this->WriteString(stderr, LINEFEED);
	va_end(argptr);
}

bool CConsoleLog::IsInfoEnabled(int level) const
{
	return true;
}

void CConsoleLog::WriteInfo(int level, const wchar_t* fmt, ...)
{
	va_list argptr;
	va_start(argptr, fmt);
	this->Write(stdout, fmt, argptr);
	va_end(argptr);
}

void CConsoleLog::WriteInfoString(int level, const wchar_t* str)
{
	fputws(str, stdout);
}

void CConsoleLog::Write(FILE* fp, const wchar_t* fmt, va_list argptr)
{
	vfwprintf_s(fp, fmt, argptr);
}

void CConsoleLog::WriteString(FILE* fp, const wchar_t* s)
{
	fputws(s, fp);
}